Skip to content

Check weight shape dimensions in ConvTranspose shape inference msrc116345#28524

Merged
yuslepukhin merged 18 commits into
mainfrom
yuslepukhin/shape_inference_msrc116345
May 26, 2026
Merged

Check weight shape dimensions in ConvTranspose shape inference msrc116345#28524
yuslepukhin merged 18 commits into
mainfrom
yuslepukhin/shape_inference_msrc116345

Conversation

@yuslepukhin
Copy link
Copy Markdown
Member

@yuslepukhin yuslepukhin commented May 15, 2026

This pull request introduces comprehensive validation and error handling improvements for the ConvTranspose operator across CPU, CUDA, WebGPU, and XNNPACK backends, as well as in shape inference and unit tests. The main focus is to ensure that invalid input shapes (especially rank-0 or rank-1 tensors) are properly detected and reported, preventing undefined behavior and improving robustness. Additionally, error messages are clarified, and several helper functions now return Status for better error propagation.

Validation and Error Handling Improvements:

  • All ConvTranspose implementations (CPU, CUDA, WebGPU) now explicitly check that input X and filter W tensors have at least 3 dimensions, returning clear error messages if not. ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R65-R79), [[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-d1bbcb0542b5acea587ac929cd6362cfd11172c522505c6db8b457a9d470c63dR273-R289), [[3]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-b615243d0702e9613bd815173108306495b0f690294001e606823b77322f6fafR22-L28))
  • The shape inference function for ConvTransposeWithDynamicPads now fails gracefully with descriptive errors if input or weight tensors have fewer than 2 dimensions. ([onnxruntime/core/graph/contrib_ops/contrib_defs.ccL62-R67](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-81f57d9adc2cce94f85a2949a895b7ff82efcc13d05e23ee6567661f0fecb7c0L62-R67))
  • Additional validation ensures that output_padding and dynamic pads have correct sizes, and that output_padding values are within ONNX-specified limits. ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R138-R153), [[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R171-R187))

Refactoring for Robustness:

  • Helper functions such as ComputePadsAndOutputShape and ComputeTransposePadAndOutputShape now return Status, allowing errors to propagate and be handled appropriately rather than causing crashes or silent failures. ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8L165-R234), [[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8L194-R262), [[3]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8L220-R282), [[4]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R291-R302))
  • All call sites (CPU, CUDA, WebGPU, XNNPACK) are updated to handle and propagate these errors using ORT_RETURN_IF_ERROR or ORT_THROW_IF_ERROR. ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R171-R187), [[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-d1bbcb0542b5acea587ac929cd6362cfd11172c522505c6db8b457a9d470c63dL362-R379), [[3]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-b615243d0702e9613bd815173108306495b0f690294001e606823b77322f6fafL48-R60), [[4]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-6a2f8672090f25850b90b266aff3c7212552fc81b14bb7b539e9e5161c9fd526L494-R497))

Unit Test Enhancements:

  • New negative tests are added to verify that rank-0 and rank-1 weight tensors are properly rejected and produce the expected error messages, increasing test coverage and reliability. ([onnxruntime/test/contrib_ops/conv_transpose_with_dynamic_pads_test.ccR22-R56](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-cb5bfc51d0c8096922eb674d142f0e970d5becd140b47bdfd7729a06a818b598R22-R56))

Minor Code Quality Improvements:

  • Improved memory management in the CPU implementation by wrapping the allocated buffer in BufferUniquePtr immediately to prevent leaks if exceptions are thrown. ([onnxruntime/core/providers/cpu/nn/conv_transpose.ccR79-R89](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-0dcb5a9c8ba0c4e67940e9d77f77cb706bbf82d67bf6757967099b0a69c797b5R79-R89))
  • Minor includes and type safety improvements (e.g., use of SafeInt for overflow protection). ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R22), [[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R291-R302))

Summary of Most Important Changes:

1. Validation and Error Handling

  • All ConvTranspose implementations now check that input and filter tensors have at least 3 dimensions, returning clear errors if not. ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R65-R79), [[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-d1bbcb0542b5acea587ac929cd6362cfd11172c522505c6db8b457a9d470c63dR273-R289), [[3]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-b615243d0702e9613bd815173108306495b0f690294001e606823b77322f6fafR22-L28))
  • Shape inference for ConvTransposeWithDynamicPads fails with descriptive errors for invalid input or weight tensor ranks. ([onnxruntime/core/graph/contrib_ops/contrib_defs.ccL62-R67](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-81f57d9adc2cce94f85a2949a895b7ff82efcc13d05e23ee6567661f0fecb7c0L62-R67))
  • Additional checks for output_padding and dynamic pads sizes and values, with ONNX spec compliance. ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R138-R153), [[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R171-R187))

2. Error Propagation and Refactoring

  • Helper functions now return Status and propagate errors; all call sites updated to handle these errors. ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8L165-R234), [[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8L194-R262), [[3]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8L220-R282), [[4]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R291-R302), [[5]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-d1bbcb0542b5acea587ac929cd6362cfd11172c522505c6db8b457a9d470c63dL362-R379), [[6]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-b615243d0702e9613bd815173108306495b0f690294001e606823b77322f6fafL48-R60), [[7]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-6a2f8672090f25850b90b266aff3c7212552fc81b14bb7b539e9e5161c9fd526L494-R497))

3. Unit Testing

  • Added tests to ensure invalid weight tensor ranks are rejected with proper error messages. ([onnxruntime/test/contrib_ops/conv_transpose_with_dynamic_pads_test.ccR22-R56](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-cb5bfc51d0c8096922eb674d142f0e970d5becd140b47bdfd7729a06a818b598R22-R56))

4. Code Quality

  • Improved buffer management and type safety in CPU backend. ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-0dcb5a9c8ba0c4e67940e9d77f77cb706bbf82d67bf6757967099b0a69c797b5R79-R89), [[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R22), [[3]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R291-R302))

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR targets improved robustness for the contrib op ConvTransposeWithDynamicPads by hardening its schema shape inference against invalid weight (W) ranks and adding regression tests for rank-0 and rank-1 weight inputs.

Changes:

  • Added a weight-shape rank guard in convTransposeWithDynamicPadsShapeInference before accessing W.dim(1).
  • Reused a cached weight_shape for computing the output channel dimension.
  • Added two unit tests covering rank-0 (scalar) and rank-1 weights expecting failure.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
onnxruntime/core/graph/contrib_ops/contrib_defs.cc Adds a weight rank check in the ConvTransposeWithDynamicPads shape inference function and uses a cached weight_shape in channel inference.
onnxruntime/test/contrib_ops/conv_transpose_with_dynamic_pads_test.cc Adds negative tests to ensure invalid W ranks are rejected.
Comments suppressed due to low confidence (1)

onnxruntime/test/contrib_ops/conv_transpose_with_dynamic_pads_test.cc:49

  • Same as the rank-0 case: please assert an expected failure message substring for this rank-1 test so it validates the intended weight-rank rejection path (rather than any downstream shape/attribute mismatch).
  test.AddOutput<float>("Y", {}, std::vector<float>{0.0f});
  test.Run(OpTester::ExpectResult::kExpectFailure, "", {kTensorrtExecutionProvider});
}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/core/graph/contrib_ops/contrib_defs.cc Outdated
Comment thread onnxruntime/core/providers/cpu/nn/conv_transpose_attributes.h Outdated
@yuslepukhin yuslepukhin requested a review from edgchen1 May 18, 2026 18:51
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Comment thread onnxruntime/core/providers/cpu/nn/conv_transpose_attributes.h
Comment thread onnxruntime/core/providers/cpu/nn/conv_transpose.cc Outdated
@yuslepukhin yuslepukhin changed the title Check weight shape dimensions in ConvTranspose shape inference Check weight shape dimensions in ConvTranspose shape inference msrc116345 May 18, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 8 comments.

Comments suppressed due to low confidence (1)

onnxruntime/core/providers/cuda/nn/conv_transpose.cc:382

  • For dynamic_padding=true, pads are populated from the Pads input without validating that its element count matches 2*kernel_shape.size(). ComputePadsAndOutputShape writes to p_pads->at(dim) / at(rank+dim), so a too-short Pads tensor can trigger out_of_range/exception instead of a clean INVALID_ARGUMENT Status. Consider validating Pads rank/size up-front (similar to the CPU PrepareForCompute path) before calling ComputePadsAndOutputShape.
    ORT_RETURN_IF_ERROR(conv_transpose_attrs_.ComputePadsAndOutputShape(input_shape, num_output_channels, kernel_shape,
                                                                        strides, dilations, local_output_padding, N, &pads, &y_dims, channels_last));

    s_.y_dims = gsl::make_span(y_dims);
    s_.Y = context->Output(0, s_.y_dims);

Comment thread onnxruntime/core/providers/cpu/nn/conv_transpose_attributes.h
Comment thread onnxruntime/core/providers/cpu/nn/conv_transpose_attributes.h
Comment thread onnxruntime/core/providers/webgpu/nn/conv_transpose.cc
Comment thread onnxruntime/test/providers/cuda/nhwc/conv_transpose_test.cc
Comment thread onnxruntime/core/graph/contrib_ops/contrib_defs.cc Outdated
Comment thread onnxruntime/core/providers/cuda/nn/conv_transpose.cc
Comment thread onnxruntime/core/providers/webgpu/nn/conv_transpose.cc
Comment thread onnxruntime/core/providers/xnnpack/nn/conv_base.cc
@yuslepukhin yuslepukhin requested a review from Copilot May 19, 2026 00:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.

Comment thread onnxruntime/test/providers/cuda/nhwc/conv_transpose_test.cc Outdated
Comment thread onnxruntime/test/providers/cuda/nhwc/conv_transpose_test.cc
Comment thread onnxruntime/test/providers/cuda/nhwc/conv_transpose_test.cc
Comment thread onnxruntime/core/providers/cpu/nn/conv_transpose_attributes.h
Comment thread onnxruntime/core/providers/cpu/nn/conv_transpose_attributes.h
Comment thread onnxruntime/core/providers/cuda/nn/conv_transpose.cc Outdated
@yuslepukhin yuslepukhin requested a review from Copilot May 19, 2026 18:21
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Comment thread onnxruntime/core/graph/contrib_ops/contrib_defs.cc
Comment thread onnxruntime/core/providers/webgpu/nn/conv_transpose.cc
Comment thread onnxruntime/core/providers/cpu/nn/conv_transpose_attributes.h
Comment thread onnxruntime/core/providers/cpu/nn/conv_transpose_attributes.h
justinchuby pushed a commit to onnx/onnx that referenced this pull request May 19, 2026
This pull request strengthens shape inference and error handling for the
`MaxUnpool` and `ConvTranspose` ONNX operators. It adds stricter input
validation to prevent silent failures or crashes when given invalid
input shapes, and introduces new tests to ensure these cases are
properly handled.

**Shape inference validation improvements:**

* Added a check in `maxUnpoolShapeInference` to ensure the indices input
exists and has at least 2 dimensions, raising a shape inference error
otherwise.
* In `convTransposeShapeInference`, added explicit checks to ensure both
the input tensor and the weight tensor have at least 2 dimensions,
raising clear errors if not.
* Updated logic in `convTransposeShapeInference` to consistently use the
local `weight_shape` variable for shape calculations and checks,
improving code clarity and correctness.
[[1]](diffhunk://#diff-b9e98b88715e8a7e56c0dfa317f21a39582f919c87185f827c4a011ad9010639L1124-R1141)
[[2]](diffhunk://#diff-b9e98b88715e8a7e56c0dfa317f21a39582f919c87185f827c4a011ad9010639L1194-R1206)

**Test coverage enhancements:**

* Added new unit tests to verify that shape inference raises appropriate
errors for invalid input shapes, such as rank-0 or rank-1
indices/weights in `MaxUnpool` and `ConvTranspose`.

**References**

microsoft/onnxruntime#28524
microsoft/onnxruntime#28550

---------

Signed-off-by: Dmitri Smirnov <dmitrism@microsoft.com>
@yuslepukhin yuslepukhin requested a review from Copilot May 19, 2026 20:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Comment thread onnxruntime/core/providers/cpu/nn/conv_transpose_attributes.h
Comment thread onnxruntime/core/providers/webgpu/nn/conv_transpose.cc
Comment thread onnxruntime/test/providers/cuda/nhwc/conv_transpose_test.cc
yuslepukhin added a commit that referenced this pull request May 21, 2026
This pull request enhances the robustness of the `MaxUnpool` operator in
ONNX Runtime by adding additional input validation and expanding test
coverage for invalid input scenarios. The changes improve error handling
for mismatched shapes, invalid dimensions, and other edge cases,
ensuring the operator fails gracefully and predictably when given
incorrect inputs.

**Operator input validation improvements:**

* Added runtime checks in `MaxUnpool::Compute` to ensure the
`kernel_shape` rank matches the expected pooling dimensions, and that
the indices tensor is present and correctly shaped.
* Added validation to ensure that computed output dimensions are
positive, with descriptive error messages if not.
* Enforced that the `output_shape` tensor, if provided, must have the
same number of elements as the rank of the input tensor.

**Test coverage enhancements:**

* Introduced multiple new tests in `unpool_op_test.cc` to cover invalid
input cases, including mismatched indices shapes, rank-0 and rank-2
input tensors, negative indices, and incorrect `output_shape` element
counts. These tests confirm that the operator fails with appropriate
error messages in these scenarios.

**References**
onnx/onnx#7997
#28524
Comment thread onnxruntime/core/providers/cpu/nn/conv_transpose.cc Outdated
Comment thread onnxruntime/core/graph/contrib_ops/contrib_defs.cc
Comment thread onnxruntime/core/providers/cpu/nn/conv_transpose_attributes.h Outdated
edgchen1
edgchen1 previously approved these changes May 26, 2026
Comment thread onnxruntime/core/providers/cuda/nn/conv_transpose.cc Outdated
Comment thread onnxruntime/core/providers/cpu/nn/conv_transpose_attributes.h Outdated
@yuslepukhin yuslepukhin enabled auto-merge (squash) May 26, 2026 20:36
tadani3 pushed a commit to tadani3/onnx that referenced this pull request May 26, 2026
This pull request strengthens shape inference and error handling for the
`MaxUnpool` and `ConvTranspose` ONNX operators. It adds stricter input
validation to prevent silent failures or crashes when given invalid
input shapes, and introduces new tests to ensure these cases are
properly handled.

**Shape inference validation improvements:**

* Added a check in `maxUnpoolShapeInference` to ensure the indices input
exists and has at least 2 dimensions, raising a shape inference error
otherwise.
* In `convTransposeShapeInference`, added explicit checks to ensure both
the input tensor and the weight tensor have at least 2 dimensions,
raising clear errors if not.
* Updated logic in `convTransposeShapeInference` to consistently use the
local `weight_shape` variable for shape calculations and checks,
improving code clarity and correctness.
[[1]](diffhunk://#diff-b9e98b88715e8a7e56c0dfa317f21a39582f919c87185f827c4a011ad9010639L1124-R1141)
[[2]](diffhunk://#diff-b9e98b88715e8a7e56c0dfa317f21a39582f919c87185f827c4a011ad9010639L1194-R1206)

**Test coverage enhancements:**

* Added new unit tests to verify that shape inference raises appropriate
errors for invalid input shapes, such as rank-0 or rank-1
indices/weights in `MaxUnpool` and `ConvTranspose`.

**References**

microsoft/onnxruntime#28524
microsoft/onnxruntime#28550

---------

Signed-off-by: Dmitri Smirnov <dmitrism@microsoft.com>
Signed-off-by: Tommaso Adani <tommasoadani@microsoft.com>
@yuslepukhin yuslepukhin merged commit 12916e9 into main May 26, 2026
88 checks passed
@yuslepukhin yuslepukhin deleted the yuslepukhin/shape_inference_msrc116345 branch May 26, 2026 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants